/**
 * 
 */
package com.angis.fx.activity.map;

import java.io.IOException;
import java.util.UUID;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.angis.fx.activity.Enforcement;
import com.angis.fx.activity.R;
import com.angis.fx.activity.login.LoginActivity;
import com.angis.fx.activity.service.NewChangsuoService;
import com.angis.fx.data.AreaType;
import com.angis.fx.util.GeoLatLng;
import com.angis.fx.util.SpinnerUtil;

/**
 * @author Han
 *
 */
public class NewChangsuoActivity extends Activity {
	
	
	private TextView mNameText;
	
	private Spinner mTypeSpinner;
	
	private Spinner mAreaSpinner1;
	
	private Spinner mAreaSpinner2;
	
	private TextView mAddressText;
	
	private TextView mPermitCodeText;
	
	private TextView mPermitWordText;
	
	private TextView mManagerText;
	
	private TextView mContactText;
	
	private Spinner mETypeSpinner;
	
	private Spinner mBWaySpinner;
	
	private GeoLatLng mGeoLatLng;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newchangsuo);
		initActivity();
		setValue();
		Button lSubmitButton = (Button) findViewById(R.id.csdetailsbsubmit);
		lSubmitButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {		
				boolean isDataCorrect = checkData();
				if(isDataCorrect){
					StringBuilder lBuilder = new StringBuilder();
					String csId = UUID.randomUUID().toString();
					lBuilder.append(csId).append("^").append(mGeoLatLng.longitude).append("^").append(mGeoLatLng.latitude)
							.append("^").append(mNameText.getText()).append("^").append(mTypeSpinner.getSelectedItem().toString())
							.append("^").append(getAreaCode(mAreaSpinner2.getSelectedItem().toString()));
					if (mAddressText.getText()!= null){
						lBuilder.append("^").append(mAddressText.getText().toString());
					} else {
						lBuilder.append("^").append("");
					}
					if (mPermitWordText.getText()!= null){
						lBuilder.append("^").append(mPermitWordText.getText().toString());
					} else {
						lBuilder.append("^").append("");
					}
					if (mPermitCodeText.getText()!= null){
						lBuilder.append("^").append(mPermitCodeText.getText().toString());
					} else {
						lBuilder.append("^").append("");
					}
					if (mManagerText.getText()!= null){
						lBuilder.append("^").append(mManagerText.getText().toString());
					} else {
						lBuilder.append("^").append("");
					}
					if (mContactText.getText()!= null){
						lBuilder.append("^").append(mContactText.getText().toString());
					} else {
						lBuilder.append("^").append("");
					}
					lBuilder.append("^").append(mETypeSpinner.getSelectedItem().toString())
							.append("^").append(mBWaySpinner.getSelectedItem().toString())
							.append("^").append(LoginActivity.g_username);
					
					Intent lIntent = new Intent(NewChangsuoActivity.this, NewChangsuoService.class);
					lIntent.putExtra("csinfo", lBuilder.toString());
					startService(lIntent);
					Toast.makeText(NewChangsuoActivity.this, "正在上传场所信息", Toast.LENGTH_SHORT).show();
					try{
						Thread.sleep(2000);
					} catch(Exception e){
						
					}
					NewChangsuoActivity.this.finish();
				}
				
			}
		});
	}
	
	private String getAreaCode(String string) {
		for (AreaType lType : Enforcement.mAreaTypes){
			if (string.equals(lType.getName()))
				return lType.getCode();
		}
		return "";
	}
	
	private boolean checkData(){
		if(mNameText.getText() == null || mNameText.getText().toString().trim().equals(""))
		{
			Toast.makeText(NewChangsuoActivity.this, "场所名称不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if(mTypeSpinner.getSelectedItem().toString() == null || mTypeSpinner.getSelectedItem().toString().trim().equals(""))
		{
			Toast.makeText(NewChangsuoActivity.this, "场所类型不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if(mAreaSpinner2.getSelectedItem().toString() == null || mAreaSpinner2.getSelectedItem().toString().trim().equals(""))
		{
			Toast.makeText(NewChangsuoActivity.this, "场所区域不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private void initActivity() {
		mGeoLatLng = (GeoLatLng)getIntent().getExtras().get("geopoint");
		mNameText = (TextView) findViewById(R.id.csdetailsname);
		mAddressText = (TextView) findViewById(R.id.csdetailsaddress);
		mAreaSpinner1 = (Spinner) findViewById(R.id.csdetailsarea1);
		mAreaSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				SpinnerUtil.setChildAreaCodes(NewChangsuoActivity.this, mAreaSpinner2, mAreaSpinner1.getSelectedItem().toString());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				SpinnerUtil.setChildAreaCodes(NewChangsuoActivity.this, mAreaSpinner2, "");
				
			}
		});
		mAreaSpinner2 = (Spinner) findViewById(R.id.csdetailsarea2);
		mTypeSpinner = (Spinner) findViewById(R.id.csdetailstype);
		mManagerText = (TextView) findViewById(R.id.csdetailspersonincharge);
		mContactText = (TextView) findViewById(R.id.csdetailscontact);
		mPermitCodeText = (TextView) findViewById(R.id.csdetailslicenseno);
		mPermitWordText = (TextView) findViewById(R.id.csdetailslicenseword);
		mETypeSpinner = (Spinner) findViewById(R.id.csdetailset);
		mBWaySpinner = (Spinner) findViewById(R.id.csdetailsbw);
	}
	
	private void setValue() {
		try {
			SpinnerUtil.setAreaCodes(this, mAreaSpinner1);
			SpinnerUtil.setChangsuoType(this, mTypeSpinner);
			SpinnerUtil.setBusiWay(this, mBWaySpinner);
			SpinnerUtil.setEconoType(this, mETypeSpinner);
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
